/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import control.Application;
import entity.Entity;
import entity.Employee;
import java.awt.Image;
import java.util.Collection;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Juan
 */
public class FLogin extends javax.swing.JFrame {

    /**
     * Creates new form FLogin
     */
    public FLogin() {
        initComponents();
        setLocationRelativeTo(null);
        ImageIcon alpha1Icon = new ImageIcon("ALPHA1SA.jpg");
        Image alpha1Image = alpha1Icon.getImage();
        alpha1Icon.setImage(alpha1Image.getScaledInstance(380, 176, Image.SCALE_DEFAULT));
        LImage.setIcon(alpha1Icon);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        TFUsername = new javax.swing.JTextField();
        BLogin = new javax.swing.JButton();
        BCancel = new javax.swing.JButton();
        PFPassword = new javax.swing.JPasswordField();
        LImage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema de Información Alpha1");
        setResizable(false);

        jLabel1.setText("Nombre de Usuario:");

        jLabel2.setText("Contraseña:");

        TFUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TFUsernameActionPerformed(evt);
            }
        });
        TFUsername.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                TFUsernameFocusGained(evt);
            }
        });

        BLogin.setText("Ingresar");
        BLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BLoginActionPerformed(evt);
            }
        });

        BCancel.setText("Cancelar");
        BCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelActionPerformed(evt);
            }
        });

        PFPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PFPasswordActionPerformed(evt);
            }
        });
        PFPassword.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                PFPasswordFocusGained(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(TFUsername)
                    .addComponent(PFPassword)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BLogin)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BCancel)))
                        .addGap(0, 226, Short.MAX_VALUE))
                    .addComponent(LImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LImage, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PFPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BLogin)
                    .addComponent(BCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelActionPerformed
        dispose();
    }//GEN-LAST:event_BCancelActionPerformed

    private void BLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BLoginActionPerformed
        String login = TFUsername.getText();
        String password = String.valueOf(PFPassword.getPassword());
        if (login == null || password == null ||
                login.length() == 0 || password.length() == 0) {
            return;
        }
        Employee request = null;
        
        Collection<Entity> employees = Application.EMPLOYEE_MANAGER.getCollection();
        for (Entity entity : employees) {
            Employee employee = (Employee) entity;
            if (employee.getLogin().equals(login) &&
                    employee.getPassword().equals(password)) {
                request = employee;
                break;
            }
        }
        
        if (request != null) {
            Application.CURRENT_EMPLOYEE = request;
            if (request.getRole() == Employee.ADMIN) {
                Application.F_MAIN.initilizeAdmin();
            } else {
                Application.F_MAIN.initializeSecretary();
            }
            Application.F_MAIN.setVisible(true);
            setVisible(false);
        } else {
            JOptionPane.showMessageDialog(this,
                    "Los datos de login proporcionados no son correctos.",
                    "Error de login", JOptionPane.ERROR_MESSAGE);
            TFUsername.requestFocusInWindow();
        }
    }//GEN-LAST:event_BLoginActionPerformed

    private void TFUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TFUsernameActionPerformed
        BLoginActionPerformed(evt);
    }//GEN-LAST:event_TFUsernameActionPerformed

    private void PFPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PFPasswordActionPerformed
        BLoginActionPerformed(evt);
    }//GEN-LAST:event_PFPasswordActionPerformed

    private void TFUsernameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_TFUsernameFocusGained
        TFUsername.selectAll();
    }//GEN-LAST:event_TFUsernameFocusGained

    private void PFPasswordFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PFPasswordFocusGained
        PFPassword.selectAll();
    }//GEN-LAST:event_PFPasswordFocusGained

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancel;
    private javax.swing.JButton BLogin;
    private javax.swing.JLabel LImage;
    private javax.swing.JPasswordField PFPassword;
    private javax.swing.JTextField TFUsername;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
